package cn.brilliantfuture.commentry.data.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.data.DataModelMessageRemote;
import cn.brilliantfuture.commentry.data.DataModelMessageWebService;
import cn.brilliantfuture.commentry.entity.DataModels;
import cn.brilliantfuture.commentry.util.BaseMessageImpl;

/**
 * 
 * Title: Communication Entry
 * 
 * Author: LiTian
 * 
 * Date: 2012-2-24
 * 
 * Description:
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.data.DataModelMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/DataModelMessageWebService")
public class DataModelMessageImpl extends BaseMessageImpl implements DataModelMessageRemote, DataModelMessageWebService {

	@PersistenceContext(unitName = "commentry-jpa")
	EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public String applyDataModel(Integer dataModelType) {

		List<DataModels> list = entityManager.createQuery("select dm from DataModels dm where dm.deleted = 0 and dm.modelType = " + dataModelType).getResultList();

		StringBuffer sb = new StringBuffer();

		if (null != list)
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					if (i < list.size() - 1) {
						sb.append(list.get(i).getDataModelId());
						sb.append(",");
						sb.append(String.valueOf(list.get(i).getUpperLimit().floatValue() * 100).split("\\.")[0]);
						sb.append(",");
						sb.append(String.valueOf(list.get(i).getLowerLimit().floatValue() * 100).split("\\.")[0]);
						sb.append(",");
						sb.append(String.valueOf(list.get(i).getUpperAlarm().floatValue() * 100).split("\\.")[0]);
						sb.append(",");
						sb.append(String.valueOf(list.get(i).getLowerAlarm().floatValue() * 100).split("\\.")[0]);
						sb.append("|");
					} else {
						sb.append(list.get(i).getDataModelId());
						sb.append(",");
						sb.append(String.valueOf(list.get(i).getUpperLimit().floatValue() * 100).split("\\.")[0]);
						sb.append(",");
						sb.append(String.valueOf(list.get(i).getLowerLimit().floatValue() * 100).split("\\.")[0]);
						sb.append(",");
						sb.append(String.valueOf(list.get(i).getUpperAlarm().floatValue() * 100).split("\\.")[0]);
						sb.append(",");
						sb.append(String.valueOf(list.get(i).getLowerAlarm().floatValue() * 100).split("\\.")[0]);
					}
				}

				return sb.toString();
			}

		return null;
	}

}
